package com.example.sensor;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import java.util.List;

public class MainActivity extends Activity implements SensorEventListener{
    SensorManager sm;
    TextView tvOrientation;
    TextView tvMagnetic;
    TextView tvLight;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvOrientation = (TextView) findViewById(R.id.orientation);
        tvLight = (TextView) findViewById(R.id.light);
        tvMagnetic = (TextView) findViewById(R.id.field);
        sm = (SensorManager) getSystemService(SENSOR_SERVICE);
        // 连接传感器模拟器
        List<Sensor> sensors = sm.getSensorList(Sensor.TYPE_ALL);
        for(Sensor s : sensors)
        {
            Log.d("fff",s.toString());
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        sm.registerListener(this,
                sm.getDefaultSensor(Sensor.TYPE_ORIENTATION),
                SensorManager.SENSOR_DELAY_GAME);
        sm.registerListener(this,
                sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
                SensorManager.SENSOR_DELAY_GAME);
        sm.registerListener(this,
                sm.getDefaultSensor(Sensor.TYPE_LIGHT),
                SensorManager.SENSOR_DELAY_GAME);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        float[] values = event.values;
        // // 真机上获取触发event的传感器类型

        int sensorType = event.sensor.getType();
        StringBuilder sb = null;
        // 判断是哪个传感器发生改变
        switch (sensorType)
        {
            // 方向传感器
            case Sensor.TYPE_ORIENTATION:
                sb = new StringBuilder();
                sb.append("绕Z轴转过的角度：");
                sb.append(values[0]);
                sb.append(
                        "绕X轴转过的角度：");
                sb.append(values[1]);
                sb.append(
                        "绕Y轴转过的角度：");
                sb.append(values[2]);
                tvOrientation.setText(sb.toString());
                break;
            // 磁场传感器
            case Sensor.TYPE_MAGNETIC_FIELD:
                sb = new StringBuilder();
                sb.append("X方向上的角度：");
                sb.append(values[0]);
                sb.append(
                        "Y方向上的角度：");
                sb.append(values[1]);
                sb.append(
                        "Z方向上的角度：");
                sb.append(values[2]);
                tvMagnetic.setText(sb.toString());
                break;

            // 光传感器
            case Sensor.TYPE_LIGHT:
                sb = new StringBuilder();
                sb.append("当前光的强度为：");
                sb.append(values[0]);
                tvLight.setText(sb.toString());
                break;
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {

    }
}
